﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
using System.Windows.Media;
using UmlControlLibrary;

namespace UmlControlLibrary
{
    public class UmlDesignerElementContextMenu:UmlContextMenu
    {
        public UmlDesignerElementContextMenu()
            : base()
        {
            AddChild(GetMenuItem(new Binding("PropertyLabel"), null));
            AddChild(GetMenuSeparator());
            AddChild(GetGroupByMenu());
            MenuItem item = GetSubMenu("Add");
            item.Items.Add(GetMenuPlaceHolder("Add"));
            AddChild(item);
            AddChild(GetMenuPlaceHolder("Delete"));
            AddChild(GetMenuPlaceHolder("Properties"));
            AddChild(GetMenuSeparator());
        }

        protected MenuItem GetGroupByMenu()
        {
            MenuItem item = GetSubMenu("Group By");
            PropertyGroupingHelper.GetPropertyListDelegate currentGrouper = PropertyGroupingHelper.GetInstance().GetPropertyListFunc;
            foreach (string grouper in PropertyGroupingHelper.GetInstance().RegisteredGroupers.Keys)
            {
                PropertyGroupingHelper.GetPropertyListDelegate grouperDelegate = PropertyGroupingHelper.GetInstance().RegisteredGroupers[grouper];
                MenuItem grouperItem = new MenuItem();
                grouperItem.Header = grouper;
                grouperItem.Tag = grouperDelegate;
                grouperItem.Click += new RoutedEventHandler(grouperItem_Click);
                grouperItem.IsCheckable = true;
                grouperItem.IsChecked = grouperDelegate == currentGrouper;
                item.Items.Add(grouperItem);
            }
            return item;
        }

        void grouperItem_Click(object sender, RoutedEventArgs e)
        {
            PropertyGroupingHelper.SetGrouper((sender as MenuItem).Tag as PropertyGroupingHelper.GetPropertyListDelegate);
            foreach (MenuItem item in ((sender as MenuItem).Parent as MenuItem).Items)
                item.IsChecked = item == sender;
        }
    }
}
